package cn.Alibaba.pet.service.impl;

import cn.Alibaba.pet.domain.PetType;
import cn.Alibaba.pet.mapper.PetTypeMapper;
import cn.Alibaba.pet.service.IPetTypeService;
import cn.Alibaba.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {

    @Autowired
    private PetTypeMapper typeMapper;

    @Override
    public List<PetType> typeTree() {

        List<PetType> petTypes = typeMapper.loadAll();

        HashMap<Long, PetType> map = new HashMap<>();

        for (PetType petType : petTypes) {

            map.put(petType.getId(),petType);

        }

        List<PetType> typeTree = new ArrayList<>();

        for (PetType petType : petTypes) {
            if (petType.getPid()==null){
                typeTree.add(petType);
            }else {
                Long pid= petType.getPid();
                PetType petType1 = map.get(pid);
                petType1.getChildren().add(petType);
            }

        }

        return typeTree;
    }
}
